package com.uzai.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author jianghan
 * @date 2019-04-03 16:17
 */
public class DateUtil {

    /**
     * 获取当前时间戳 (s)
     * @return
     */
    public static int getTime(){
        return getTime(new Date());
    }

    /**
     * 获取指定时间戳 (s)
     * @param date
     * @return
     */
    public static int getTime(Date date){
        if (date==null){
            return 0;
        }
        return (int) (date.getTime()/1000);
    }

    /**
     * 获取当天的天数
     * @return
     */
    public static int getToDayNum() {
        Calendar cal = new GregorianCalendar();
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当天的开始时间
     * @return
     */
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取当天的结束时间
     * @return
     */
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 获取昨天的开始时间
     * @return
     */
    public static Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取昨天的结束时间
     * @return
     */
    public static Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取本月的开始时间
     * @return
     */
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取本月的结束时间
     * @return
     */
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }


    /**
     * 获取今年是哪一年
     * @return
     */
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    /**
     * 获取本月是哪一月
     * @return
     */
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    /**
     * 获取某个日期的开始时间
     * @param d
     * @return
     */
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取某个日期的结束时间
     * @param d
     * @return
     */
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d){
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取上月的开始时间
     * @return
     */
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取上月的结束时间
     * @return
     */
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }

    public static String getMinute() {
        return DateFormatUtil.dateToStr(new Date(),"mm");
    }

    /**
     * 检查时间是否在指定范围
     * @param dateVal
     * @return
     */
    public static boolean inDateScope(String dateVal){
        if (StringUtils.isBlank(dateVal)){
            return true;
        }
        int now = DateUtil.getTime();
        String[] times = dateVal.split("-");
        String today = DateFormatUtil.getToDayFormatStr();
        int begin = DateFormatUtil.ymdhmsToTime(today+" "+times[0].trim());
        int end = DateFormatUtil.ymdhmsToTime(today+" "+times[1].trim());

        if (begin<=end){
            if (begin<now && now<end){
                return true;
            }
        }else {
            if (now>begin || now<end){
                return true;
            }
        }
        return false;
    }


    /**
     * 夜间延迟时间
     * @param dateVal
     * @return
     */
    public static int delayTime(String dateVal,int now){
        if (StringUtils.isBlank(dateVal)){
            return 0;
        }
        String[] times = dateVal.split("-");
        String today = DateFormatUtil.getToDayFormatStr();
        int begin = DateFormatUtil.ymdhmsToTime(today+" "+times[0].trim());
        int end = DateFormatUtil.ymdhmsToTime(today+" "+times[1].trim());

        if (begin<=end){
            if (begin<now && now<end){
                return end-now;
            }
        }else {
            if (now>begin || now<end){
                end = end+86400;
                return end-now;
            }
        }
        return 0;
    }

    // 刘总专属开始


    /**
     * 获取时间指定月的开始时间戳或者结束时间戳
     * @param month(格式：yyyy-MM)
     * @param num 0:开始时间戳  1:结束时间戳
     * @return
     */
    public static Integer getStartTimeOrEndTimeByMonth(String month, int num){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String startMonth = getEndDayOrOneDay(sdf.parse(month),0);
            String endMonth = getEndDayOrOneDay(sdf.parse(month),1);
            if(num==0){
                startMonth = startMonth + " 00:00:00";
                Date startMonthDate =   StringToDateFormat(startMonth);
                return DateUtil.getTime(startMonthDate);
            }else{
                endMonth = endMonth + " 23:59:59";
                Date endMonthDate =   StringToDateFormat(endMonth);
                return DateUtil.getTime(endMonthDate);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * String日期转date
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date StringToDateFormat(String str)  {
        SimpleDateFormat format_local = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isBlank(str)){
            return null;
        }
        Date date = null;
        try {
            date = format_local.parse(str);
        } catch (Exception e) {
        }
        return date;
    }

    /**
     * 获取时间所在月第一天和最后一天
     * @param start_time
     * @param num 0:第一天  1:最后一天
     * @return
     */
    public static String getEndDayOrOneDay(Date start_time, int num){
        String time = "";
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if(num==0){
            // 获取当前月的第一天
            cale = Calendar.getInstance();
            cale.setTime(start_time);
            cale.add(Calendar.MONTH, 0);
            cale.set(Calendar.DAY_OF_MONTH, 1);
            time = format.format(cale.getTime());
        }else{
            // 获取当前月的最后一天
            cale = Calendar.getInstance();
            cale.setTime(start_time);
            cale.add(Calendar.MONTH, 1);
            cale.set(Calendar.DAY_OF_MONTH, 0);
            time = format.format(cale.getTime());
        }
        //return Integer.parseInt(time);
        return time;
    }

    public static int getStrTime(String date){
        return getTime(StringToDateFormat(date));
    }

    /**
     * 获得指定日期的前几天
     *
     * @param specifiedDay
     * @param format:yyyy-MM-dd
     * @param days 相隔的天数
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay,String format,int days) {//可以用new Date().toLocalString()传递参数
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat(format).parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - days);

        String dayBefore = new SimpleDateFormat(format).format(c
                .getTime());
        return dayBefore;
    }

    // 刘总专属结束
}
